package com.winderinfo.yikaotianxia.ui.mockexam;

import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.base.BaselanFragment;
import com.winderinfo.yikaotianxia.core.utils.LogUtil;
import com.winderinfo.yikaotianxia.core.utils.MapUtil;

import java.util.List;

import butterknife.BindView;


public class AnswerFragment extends BaselanFragment {

    @BindView(R.id.sing_type)
    TextView singType;
    @BindView(R.id.sing_sum)
    TextView singSum;
    @BindView(R.id.sing_num)
    TextView singNum;
    @BindView(R.id.sing_titleTitl)
    TextView singTitleTitl;
    @BindView(R.id.sing_a)
    RadioButton singA;
    @BindView(R.id.sing_b)
    RadioButton singB;
    @BindView(R.id.sing_c)
    RadioButton singC;
    @BindView(R.id.sing_d)
    RadioButton singD;
    @BindView(R.id.sing_group)
    RadioGroup singGroup;
    @BindView(R.id.sing_ta)
    TextView singTa;
    @BindView(R.id.sing_tb)
    TextView singTb;
    @BindView(R.id.sing_tc)
    TextView singTc;
    @BindView(R.id.sing_td)
    TextView singTd;
    @BindView(R.id.sing_dan)
    LinearLayout singDan;
    @BindView(R.id.sing_duo)
    LinearLayout singDuo;
    @BindView(R.id.du_a)
    CheckBox duA;
    @BindView(R.id.du_ta)
    TextView duTa;
    @BindView(R.id.du_b)
    CheckBox duB;
    @BindView(R.id.du_tb)
    TextView duTb;
    @BindView(R.id.du_c)
    CheckBox duC;
    @BindView(R.id.du_tc)
    TextView duTc;
    @BindView(R.id.du_d)
    CheckBox duD;
    @BindView(R.id.du_td)
    TextView duTd;

    private int ids;
    private int cardid;
    private int titleTypeId;
    private AnswerBean.RowsBean list;

    @Override
    protected int layoutResID() {
        return R.layout.fragment_single;
    }


    @Override
    protected void initView(View view) {

    }


    @Override
    protected void loadData() {
        int sum = getArguments().getInt("sum");
        int num = getArguments().getInt("num");

        list = (AnswerBean.RowsBean) getArguments().getSerializable("list");
        cardid = getArguments().getInt("cardid", 0);

        //设置值
        ids = list.getId();
        singTitleTitl.setText(list.getTitleTitl());
        singNum.setText(sum + "");
        singSum.setText((num + 1) + "");

        //设置选项
        titleTypeId = list.getTitleTypeId();
        List<AnswerBean.RowsBean.YkAnswersBean> ykAnswers = list.getYkAnswers();


        getCC();

        if (titleTypeId == 1) {//单选
            singType.setText("单选题");
            singDan.setVisibility(View.VISIBLE);
            //单选
            CardBean cardBean = MapUtil.cardBeanMap.get(ids + "");
            if (cardBean != null) {
                String remarks = cardBean.getRemarks();
                if (remarks.equals("A")) {
                    singA.setChecked(true);
                    singA.setTextColor(getActivity().getResources().getColor(R.color.colorWhite));
                }
                if (remarks.equals("B")) {
                    singB.setChecked(true);
                    singB.setTextColor(getActivity().getResources().getColor(R.color.colorWhite));
                }
                if (remarks.equals("C")) {
                    singC.setChecked(true);
                    singC.setTextColor(getActivity().getResources().getColor(R.color.colorWhite));
                }
                if (remarks.equals("D")) {
                    singD.setChecked(true);
                    singD.setTextColor(getActivity().getResources().getColor(R.color.colorWhite));
                }
            }
        } else if (titleTypeId == 2) {//多选
            singType.setText("多选题");
            singDuo.setVisibility(View.VISIBLE);
        }

        //单选
        if (ykAnswers.size() > 0) {
            if (ykAnswers.size() == 1) {
                singTa.setText(list.getYkAnswers().get(0).getAnswerContent());
                duTa.setText(list.getYkAnswers().get(0).getAnswerContent());
            } else if (ykAnswers.size() == 2) {
                singTa.setText(list.getYkAnswers().get(0).getAnswerContent());
                singTb.setText(list.getYkAnswers().get(1).getAnswerContent());
                duTa.setText(list.getYkAnswers().get(0).getAnswerContent());
                duTb.setText(list.getYkAnswers().get(1).getAnswerContent());
            } else if (ykAnswers.size() == 3) {
                singTa.setText(list.getYkAnswers().get(0).getAnswerContent());
                singTb.setText(list.getYkAnswers().get(1).getAnswerContent());
                singTc.setText(list.getYkAnswers().get(2).getAnswerContent());
                duTa.setText(list.getYkAnswers().get(0).getAnswerContent());
                duTb.setText(list.getYkAnswers().get(1).getAnswerContent());
                duTc.setText(list.getYkAnswers().get(2).getAnswerContent());
            } else if (ykAnswers.size() == 4) {
                singTa.setText(list.getYkAnswers().get(0).getAnswerContent());
                singTb.setText(list.getYkAnswers().get(1).getAnswerContent());
                singTc.setText(list.getYkAnswers().get(2).getAnswerContent());
                singTd.setText(list.getYkAnswers().get(3).getAnswerContent());
                duTa.setText(list.getYkAnswers().get(0).getAnswerContent());
                duTb.setText(list.getYkAnswers().get(1).getAnswerContent());
                duTc.setText(list.getYkAnswers().get(2).getAnswerContent());
                duTd.setText(list.getYkAnswers().get(3).getAnswerContent());
            }
        }
        //单选
        singGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (!singA.isChecked()) {
                    singA.setTextColor(getActivity().getResources().getColor(R.color.colorToastBlack));
                }
                if (!singB.isChecked()) {
                    singB.setTextColor(getActivity().getResources().getColor(R.color.colorToastBlack));
                }
                if (!singC.isChecked()) {
                    singC.setTextColor(getActivity().getResources().getColor(R.color.colorToastBlack));
                }
                if (!singD.isChecked()) {
                    singD.setTextColor(getActivity().getResources().getColor(R.color.colorToastBlack));
                }

                CardBean cardBean = new CardBean();
                cardBean.setTitleId(ids);
                cardBean.setScoreId(cardid + "");
                cardBean.setUserId("1");
                cardBean.setTitleType(titleTypeId + "");
                if (singA.isChecked()) {
                    singA.setTextColor(getActivity().getResources().getColor(R.color.colorWhite));
                    cardBean.setRemarks("A");
                    if (list.getZhengque().equals("A")) {
                        cardBean.setComplete("1");
                    } else {
                        cardBean.setComplete("2");
                    }
                } else if (singB.isChecked()) {
                    singB.setTextColor(getActivity().getResources().getColor(R.color.colorWhite));
                    cardBean.setRemarks("B");
                    if (list.getZhengque().equals("B")) {
                        cardBean.setComplete("1");
                    } else {
                        cardBean.setComplete("2");
                    }
                } else if (singC.isChecked()) {
                    singC.setTextColor(getActivity().getResources().getColor(R.color.colorWhite));
                    cardBean.setRemarks("C");
                    if (list.getZhengque().equals("C")) {
                        cardBean.setComplete("1");
                    } else {
                        cardBean.setComplete("2");
                    }
                } else if (singD.isChecked
                        ()) {
                    singD.setTextColor(getActivity().getResources().getColor(R.color.colorWhite));
                    cardBean.setRemarks("D");
                    if (list.getZhengque().equals("D")) {
                        cardBean.setComplete("1");
                    } else {
                        cardBean.setComplete("2");
                    }
                }

                MapUtil.cardBeanMap.put(ids + "", cardBean);

            }
        });

        //多选
        duA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getAA();
            }
        });
        duB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getAA();
            }
        });
        duC.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getAA();
            }
        });
        duD.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getAA();
            }
        });
    }

    private void getCC() {

        CardBean cardBean1 = MapUtil.cardBeanMap.get(ids + "");
        if (cardBean1 == null) {
            CardBean cardBean = new CardBean();
            cardBean.setTitleId(ids);
            cardBean.setScoreId(cardid + "");
            cardBean.setUserId("1");
            cardBean.setTitleType(titleTypeId + "");
            cardBean.setRemarks("");
            cardBean.setComplete("0");
            MapUtil.cardBeanMap.put(ids + "", cardBean);
            LogUtil.i("idididid", ids + "");
        }

    }

    private void getAA() {
        CardBean cardBean = new CardBean();
        cardBean.setTitleId(ids);
        cardBean.setScoreId(cardid + "");
        cardBean.setComplete("1");
        cardBean.setUserId("1");
        cardBean.setTitleType(titleTypeId + "");
        String ss = "";
        if (duA.isChecked()) {
            ss += "A,";
            duA.setTextColor(getActivity().getResources().getColor(R.color.colorWhite));
        }
        if (duB.isChecked()) {
            ss += "B,";
            duB.setTextColor(getActivity().getResources().getColor(R.color.colorWhite));
        }
        if (duC.isChecked()) {
            ss += "C,";
            duC.setTextColor(getActivity().getResources().getColor(R.color.colorWhite));
        }
        if (duD.isChecked()) {
            ss += "D,";
            duD.setTextColor(getActivity().getResources().getColor(R.color.colorWhite));
        }
        if (!duA.isChecked()) {
            duA.setTextColor(getActivity().getResources().getColor(R.color.colorToastBlack));
        }
        if (!duB.isChecked()) {
            duB.setTextColor(getActivity().getResources().getColor(R.color.colorToastBlack));
        }
        if (!duC.isChecked()) {
            duC.setTextColor(getActivity().getResources().getColor(R.color.colorToastBlack));
        }
        if (!duD.isChecked()) {
            duD.setTextColor(getActivity().getResources().getColor(R.color.colorToastBlack));
        }
        String[] split = ss.split(",");
        String a = "";
        for (int i = 0; i < split.length; i++) {
            if (i < split.length - 1) {
                a += split[i] + ",";
            } else {
                a += split[i];
            }
        }
        cardBean.setRemarks(a);

        if (list.getZhengque().equals(a)) {
            cardBean.setComplete("1");
        } else {
            cardBean.setComplete("2");
        }
        MapUtil.cardBeanMap.put(ids + "", cardBean);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onPause() {
        super.onPause();
    }
}
